package com.tgpms.web.common.service.ipml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.interfaces.dto.VformInterface;
import com.tgpms.util.Common;
import com.tgpms.web.common.service.FTPService;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class FTPServiceImpl implements FTPService {
	
	@Autowired
	private VformInterface vformInterface;

	@SuppressWarnings({ "unchecked" })
	@Override
	public Map<String,String> addUploadFileInfo(Map<String, String> map) {
		Map<String,String> resMap = new HashMap<String, String>();
		JSONObject checkno = new JSONObject();
		checkno.put("FILE_PROG_CODE=", map.get("fileProgCode"));
		checkno.put("FILE_KEY=", map.get("fileKey"));
		//查询vform主键
		Result findReslut = vformInterface.find("DC114", checkno.toJSONString(), "1", "100", "N", "N", "");
		System.out.println("vform查询返回结果为:  "+findReslut);
		if(findReslut==null || !findReslut.getSuccess()) {
			resMap.put("code", "-3");
			resMap.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return resMap;
		}
		if(findReslut.getData().toString().equals("{\"message\":\"login\"}")) {
			resMap.put("code", "-3");
			resMap.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
			return resMap;
		}
		Integer seqNo = 0;
		if(findReslut.getData().toString().equals("[]")) {
			seqNo = 1;
		}else {
			List<Map<String,String>> list = JSONArray.parseObject(findReslut.getData().toString(), List.class);
			list.sort((map1, map2) -> Integer.parseInt(map1.get("SEQ_NO")) - Integer.parseInt(map2.get("SEQ_NO")));
			seqNo = Integer.parseInt(list.get(list.size() -1).get("SEQ_NO")) + 1;
		}
		map.put("seqNo", seqNo+"");
		JSONObject json = new JSONObject();
		json.put("FILE_PROG_CODE", map.get("fileProgCode"));
		json.put("FILE_CAT", map.get("fileCat"));
		json.put("FILE_KEY", map.get("fileKey"));
		json.put("FILE_SHOW_NAME", map.get("fileShowName"));
		json.put("FILE_NAME", map.get("fileName"));
		json.put("FILE_SIZE", map.get("fileSize"));
		json.put("SEQ_NO", seqNo+"");
		json.put("CONFIRM_SUCCESS", "0");
		json.put("FILE_LEVEL", "1");
		json.put("UPLOAD_STATUS", "0");
		json.put("UPLOAD_DATE", Common.fromDateFormat("yyyy/MM/dd"));
		json.put("UPLOAD_USER", map.get("uploadUser"));
		System.out.println("vform增加上送参数   "+json.toString());
		//vform同步插入
		Result result = vformInterface.insert("DC114", json);
		System.out.println("vform增加返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			resMap.put("code", "-3");
			resMap.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return resMap;
		}
		String resData = result.getData().toString();
		if(resData.equals("{\"message\":\"login\"}")) {
			resMap.put("code", "-3");
			resMap.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
			return resMap;
		}
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			resMap.put("code", "-4");
			resMap.put("info", reJson.get("validateError").toString());
			return resMap;
		}else {
			resMap.put("code", "1");
			resMap.put("info", ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
		}
		return resMap;
	}

	@Override
	public List<Map<String, Object>> getFilelist(String fileKey,String fileProgCode,String dataowner) {
		JSONObject checkno = new JSONObject();
		checkno.put("FILE_KEY=", fileKey);
		checkno.put("FILE_PROG_CODE=", fileProgCode);
		//查询vform主键
		Result findReslut = vformInterface.find("DC114", checkno.toJSONString(), "1", "100", "N", "N", dataowner);
		System.out.println("vform查询返回结果为:  "+findReslut);
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		if(findReslut!=null && findReslut.getSuccess()) {
			if(!findReslut.getData().toString().equals("{\"message\":\"login\"}")) {
				if(!findReslut.getData().toString().equals("[]")) {
					list = JSONArray.parseObject(findReslut.getData().toString(), List.class);
				}
			}
		}
		return list;
	}

	@Override
	public Map<String, String> deleteFile(String fileKey, String fileProgCode, String seqNo) {
		Map<String,String> map = new HashMap<String, String>();
		JSONObject keyjson = new JSONObject();
		keyjson.put("FILE_PROG_CODE", fileProgCode);
		keyjson.put("FILE_KEY", fileKey);
		keyjson.put("SEQ_NO", seqNo);
		System.out.println("vform删除上送主键   "+keyjson.toString());
		//vform同步删除
		Result result = vformInterface.delete("DC117", keyjson.toJSONString());
		System.out.println("vform删除返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		String resData = result.getData().toString();
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			map.put("code", "-4");
			map.put("info", reJson.get("validateError").toString());
			return map;
		}else {
			map.put("code", "1");
			map.put("info", ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
		}
		return map;
	}

	@Override
	public Map<String, String> updateUploadFileInfo(Map<String, String> map) {
		Map<String,String> resMap = new HashMap<String, String>();
		JSONObject keyjson = new JSONObject();
		keyjson.put("FILE_PROG_CODE", map.get("fileProgCode"));
		keyjson.put("FILE_KEY", map.get("fileKey"));
		keyjson.put("SEQ_NO", map.get("seqNo") );
		System.out.println("vform更新上送主键   "+keyjson.toString());
		JSONObject pajson = new JSONObject();
		pajson.put("UPLOAD_STATUS", "1");
		pajson.put("CONFIRM_SUCCESS", "1");
		System.out.println("vform更新上送参数   "+pajson.toString());
		//vform同步修改
		Result result = vformInterface.update("DC114", keyjson.toString(),pajson.toString(),null,null,null);
		System.out.println("vform更新返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			resMap.put("code", "-3");
			resMap.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return resMap;
		}
		String resData = result.getData().toString();
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			resMap.put("code", "-4");
			resMap.put("info", reJson.get("validateError").toString());
			return resMap;
		}else {
			resMap.put("code", "1");
			resMap.put("info", ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
		}
		return resMap;
	}
	
	
	
}
